import axios from 'axios';

export interface ApplicationFilters {
  applicant_id?: string;
  status?: string;
  kind?: string; // leave | purchase
  apply_type?: string; // leave | overtime (only for leave kind)
  page?: number;
  page_size?: number;
}

export interface TaskSummary {
  task_id?: string;
  name?: string;
  assignee?: string;
  created?: string;
}

export interface ApplicationListResponseItem {  
  initiator_id: string;
  completed: boolean;
  business: {
    business_kind: "purchase" | "leave" | "overtime";
    id: number;
    applicant_id: string;
    status: "pending" | "approved" | "rejected" | "cancelled" | "completed";
    project_id: number | null;
  };
  current_tasks: TaskSummary[];
  process_instance_id?: string; 
}

export interface ApplicationListPayload {
  items: ApplicationListResponseItem[];
  total: number;
  page: number;
  page_size: number;
  pages: number;
}


/**
 * 获取统一申请列表（分页）
 */
export async function listApplications(filters: ApplicationFilters): Promise<ApplicationListPayload> {
  const { data } = await axios.get('/api/applications/flowable', { params: filters });
  // backend success_response => { code, message, data }
  return data.data as ApplicationListPayload;
}

/**
 * 获取申请详情
 */
export async function getApplicationDetail(id: number): Promise<any> {
  const { data } = await axios.get(`/api/applications/${id}`);
  // backend success_response => { code, message, data }
  return data.data;
}
